package com.houhanzhi.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.houhanzhi.enums.ShortLinkStateEnum;
import com.houhanzhi.manager.GroupCodeMappingManager;
import com.houhanzhi.mapper.GroupCodeMappingMapper;
import com.houhanzhi.model.GroupCodeMappingDO;
import com.houhanzhi.vo.GroupCodeMappingVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

@Repository
@Slf4j
public class GroupCodeMappingManagerImpl implements GroupCodeMappingManager {

    @Autowired
    private GroupCodeMappingMapper groupCodeMappingMapper;


    /**
     * 查询详情
     * @param groupId
     * @param mappingId
     * @param accountNo
     * @return
     */
    @Override
    public GroupCodeMappingDO findByGroupIdAndMappingId(Long groupId, Long mappingId, Long accountNo) {
        return groupCodeMappingMapper.selectOne(new QueryWrapper<GroupCodeMappingDO>()
                .eq("group_id", groupId)
                .eq("account_no", accountNo)
                .eq("id", mappingId)
                .eq("del", 0)
        );
    }

    /**
     * 新增
     * @param groupCodeMappingDO
     * @return
     */
    @Override
    public int add(GroupCodeMappingDO groupCodeMappingDO) {
        return groupCodeMappingMapper.insert(groupCodeMappingDO);
    }

    /**
     * 根据短链码删除
     * @param groupCodeMappingDO
     * @return
     */
    @Override
    public int del(GroupCodeMappingDO groupCodeMappingDO) {
        return groupCodeMappingMapper.update(null, new UpdateWrapper<GroupCodeMappingDO>()
                .eq("group_id", groupCodeMappingDO.getGroupId())
                .eq("account_no", groupCodeMappingDO.getAccountNo())
                .eq("code", groupCodeMappingDO.getCode())
                .set("del", 1)
        );
    }

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @param groupId
     * @param accountNo
     * @return
     */
    @Override
    public Map<String, Object> pageShortLinkByGroupId(int pageNum, int pageSize, Long groupId, Long accountNo) {
        Page<GroupCodeMappingDO> pageInfo = new Page<>(pageNum, pageSize);
        final IPage<GroupCodeMappingDO> groupCodeMappingDOPage = groupCodeMappingMapper.selectPage(pageInfo, new QueryWrapper<GroupCodeMappingDO>()
                .eq("group_id", groupId)
                .eq("account_no", accountNo)
                .eq("del", 0)
                .orderByDesc("gmt_create")
        );

        Map<String,Object> pageMap = new HashMap<>(3);
        pageMap.put("total_record", groupCodeMappingDOPage.getTotal());
        pageMap.put("total_page", groupCodeMappingDOPage.getPages());
        pageMap.put("current_data", groupCodeMappingDOPage.getRecords().stream()
                .map(this::beanProcess)
                .collect(Collectors.toList()));
        return pageMap;
    }

    private GroupCodeMappingVO beanProcess(GroupCodeMappingDO groupCodeMappingDO) {
        final GroupCodeMappingVO groupCodeMappingVO = new GroupCodeMappingVO();
        BeanUtils.copyProperties(groupCodeMappingDO, groupCodeMappingVO);
        return groupCodeMappingVO;
    }

    /**
     * 更新短链码状态
     * @param groupId
     * @param accountNo
     * @param shortLinkCode
     * @param shortLinkStateEnum
     * @return
     */
    @Override
    public int updateGroupCodeMappingState(Long groupId, Long accountNo, String shortLinkCode, ShortLinkStateEnum shortLinkStateEnum) {
        return groupCodeMappingMapper.update(null, new UpdateWrapper<GroupCodeMappingDO>()
                .eq("group_id", groupId)
                .eq("account_no", accountNo)
                .eq("code", shortLinkCode)
                .eq("del", 0)
                .set("state", shortLinkStateEnum.name())
        );
    }

    /**
     * 查找短链码是否存在
     * @param shortLinkCode
     * @param groupId
     * @param accountNo
     * @return
     */
    @Override
    public GroupCodeMappingDO findByCodeAndGroupId(String shortLinkCode, Long groupId, Long accountNo) {
        return groupCodeMappingMapper.selectOne(new QueryWrapper<GroupCodeMappingDO>()
                .eq("group_id", groupId)
                .eq("account_no", accountNo)
                .eq("del", 0)
                .eq("code", shortLinkCode));
    }

    /**
     * 更新短链
     * @param groupCodeMappingDO
     * @return
     */
    @Override
    public int update(GroupCodeMappingDO groupCodeMappingDO) {
        return groupCodeMappingMapper.update(null, new UpdateWrapper<GroupCodeMappingDO>()
                .eq("id", groupCodeMappingDO.getId())
                .eq("account_no", groupCodeMappingDO.getAccountNo())
                .eq("group_id", groupCodeMappingDO.getGroupId())
                .eq("del", 0)
                .set("title", groupCodeMappingDO.getTitle())
                .set("domain", groupCodeMappingDO.getDomain())
        );
    }
}
